#ifndef __F1_RACER_MOTION_PROTOCOL_H__
#define __F1_RACER_MOTION_PROTOCOL_H__
#include "XD_Racer.h"
enum COMMAND_HEAD{NORMAL_OPT = 0X60,DEBUG_OPT = 0X61,KEEP_ALIVE = 0X62,};
enum COMMAND_TYPE{SET_ZERO = 0XA0,DOWNLOAD_MOVIE_DATA = 0XA1,RUN_MOVIE_DATA = 0XA2,RUN_GAME_DATA = 0XA3,LOAD_SAVE = 0XA4};
enum ERROR_CODE{NO_ERROR_CODE = 1,};
enum COMMAND_END{END_COMMAND = 0X03}; 
#define  STOP_COMMAND 0XB0

typedef struct{
	char command[1024];
	int  len;
	 
}MotionProtocal;

static MotionProtocal MakeSetZero()
{
	MotionProtocal com;
	com.command[0] = (char)NORMAL_OPT;
	com.command[1] = (char)SET_ZERO;
	com.command[2] = (char)END_COMMAND;
	com.len = 3;
	return com;
}

static MotionProtocal MakeBeginDownloadMovieData(byte num,int count)
{
	MotionProtocal com;
	com.command[0] = (char)NORMAL_OPT;
	com.command[1] = (char)DOWNLOAD_MOVIE_DATA;
	com.command[2] = num;


	com.command[3] = *((char *)&count + 3);
	com.command[4] = *((char *)&count + 2);
	com.command[5] = *((char *)&count + 1);
	com.command[6] = *((char *)&count + 0);
	com.command[7] = END_COMMAND;
	com.len = 8;
	return com;
}
static MotionProtocal MakeDownloadMovieData(byte num,int index)
{
	MotionProtocal com;
	com.command[0] = (char)NORMAL_OPT;
	com.command[1] = (char)DOWNLOAD_MOVIE_DATA;
	com.command[2] = num;
	

	com.command[3] = *((char *)&index + 3);
	com.command[4] = *((char *)&index + 2);
	com.command[5] = *((char *)&index + 1);
	com.command[6] = *((char *)&index + 0);
	
	static short old_x = 0;
	static short old_y = 0;
	int m = 7;
	for (int i=0;i<200;i++)
	{
		int x;
		int y;
		if(index + i < gMMDataList.size())
		{
			x = gMMDataList[index + i].x2;
			y = gMMDataList[index + i].y2;
		}
		else
		{
			x = 0;
			y = 0;
		}
		short dx = x - old_x;
		short dy = y - old_y;

		com.command[m++] = *((char *)&dx + 1);
		com.command[m++] = *((char *)&dx + 0);
		com.command[m++] = *((char *)&dy + 1);
		com.command[m++] = *((char *)&dy + 0);
		old_x = x;
		old_y = y;
	}
	
	com.command[m++] = END_COMMAND;
	com.len = m;
	
	return com;
}
static MotionProtocal MakeRunMovieData(int num,byte mode)
{

	MotionProtocal com;
	com.command[0] = (char)NORMAL_OPT;
	com.command[1] = (char)RUN_MOVIE_DATA;
	com.command[2] = (char)num;
	com.command[3] = mode;
	com.command[4] = (char)END_COMMAND;
	com.len = 5;
	return com;
}

static MotionProtocal MakeRunGameData(short x,short y)
{
	static short old_x = 0;
	static short old_y = 0;
	short dx = x - old_x;
	short dy = y - old_y;
	MotionProtocal com;
	com.command[0] = NORMAL_OPT;
	com.command[1] = RUN_GAME_DATA;
	com.command[2] = *((char *)&dx + 1);
	com.command[3] = *((char *)&dx + 0);
	com.command[4] = *((char *)&dy + 1);
	com.command[5] = *((char *)&dy + 0);
	com.command[6] = END_COMMAND;
	com.len = 7;
	old_x = x;
	old_y = y;
	return com;
}

static MotionProtocal MakeDebugCommand(int type)
{
	MotionProtocal com;
	com.command[0] = (char)DEBUG_OPT;
	com.command[1] = (char)type;
	com.command[2] = (char)END_COMMAND;
	com.len = 3;
	return com;
}

static MotionProtocal MakeKeepCommand(int count)
{
	MotionProtocal com;
	com.command[0] = (char)KEEP_ALIVE;
	com.command[1] = *((char *)&count + 3);;
	com.command[2] = *((char *)&count + 2);
	com.command[3] = *((char *)&count + 1);
	com.command[4] = *((char *)&count + 0);
	com.command[5] = (char)END_COMMAND;
	com.len = 6;
	return com;

}

static MotionProtocal MakeStopCommand()
{
	MotionProtocal com;
	com.command[0] = (char)DEBUG_OPT;
	com.command[1] = (char)STOP_COMMAND;
	com.command[2] = (char)END_COMMAND;
	com.len = 3;
	return com;
}
static MotionProtocal MakeSaveCommand(int num)
{
	MotionProtocal com;
	com.command[0] = (char)NORMAL_OPT;
	com.command[1] = (char)LOAD_SAVE;
	com.command[2] = (char)num;
	com.command[3] = (char)0x00;
	com.command[4] = (char)END_COMMAND;
	com.len = 5;
	return com;

}
static MotionProtocal MakeLoadCommand(int num)
{
	MotionProtocal com;
	com.command[0] = (char)NORMAL_OPT;
	com.command[1] = (char)LOAD_SAVE;
	com.command[2] = (char)num;
	com.command[3] = (char)0x01;
	com.command[4] = (char)END_COMMAND;
	com.len = 5;
	return com;
}
#endif